package com.matu.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.matu.domain.Depart;
import com.matu.service.ServiceTisseo;

@Controller
public class HoraireLigneController {
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/horaireLigne", method = RequestMethod.GET)
	public String horaireLigne(Locale locale, Model model, HttpServletRequest request) {
		logger.info("Welcome home! The client locale is {}.", locale);
		
		ServiceTisseo sTisseo = new ServiceTisseo();
		
		/*
		 * Chercher les prochains départs d'un arret
		 */
		String idSelectedAret = (String)request.getParameter("idArret");

		ArrayList<Depart> listeDepart = new ArrayList<Depart>();
		if (idSelectedAret != null){
			
			try {
				listeDepart = sTisseo.getServiceProchainsDeparts(idSelectedAret);
			} catch (IOException e) {
		
				e.printStackTrace();
			} catch (ParseException e) {

				e.printStackTrace();
			}
		
		}
		
		model.addAttribute("listDeparts",listeDepart);
		

		
		return "horaireLigne";
	}
}
